const { Serverlet } = require('./serverlet')
const fs = require('fs')
const url = require('url')

class uploadFile extends Serverlet {
  constructor (req, res) {
    super(req, res)
  }

  get (req, res) {
    res.end('This is end!')
  }

  post (req, res) {
    var param = url.parse(req.url, true).query
    var result = {
      code: 200,
      count: 1,
      msg: '上传资源成功：' + param.fileName
    }
    res.writeHead(200, {
      'Content-Type': 'text/html;charset=utf-8'
    })
    req.pipe(fs.createWriteStream('./file/' + param.fileName, 'base64'))
    res.end(JSON.stringify(result))
  }
}

module.exports = {
  uploadFile,
}
